/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbhelper;

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author blackant
 */
public class dbconnect {

    private Connection conn;

    public void open() {
        try {
            //dk driver
            Driver driver = new org.gjt.mm.mysql.Driver();
            DriverManager.registerDriver(driver);
            //chuoi ket noi
            String conString = "jdbc:mysql://localhost:3306/java-doan";
            Properties info = new Properties();
            info.setProperty("characterEnconding", "utf8");
            info.setProperty("user", "root");
            info.setProperty("password", "");
            //ket noi
            this.conn = DriverManager.getConnection(conString, info);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void close() {
        try {
            this.conn.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public ResultSet Select(String sql)  
    {
        ResultSet rs = null;
        try{
            Statement st = (Statement) this.conn.createStatement();
            rs = st.executeQuery(sql);
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return rs;
    }

    public int Update(String sql) 
    {
        int n = -1;
        try
        {
            Statement st = (Statement) this.conn.createStatement();
            n = st.executeUpdate(sql);
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return n;
    }
    public Connection getConn()
    {
        return this.conn;
    }
}
